# -*- coding: UTF-8  -*-
import os, sys, web
from mako.template import Template
from mako.lookup import TemplateLookup
from dateutil.parser import *


def getObj():
    '''
    获取一个简单对象。
    '''
    class SomeObject(object): pass;
    obj = SomeObject();
    return obj;

def mako(objs, filepath, filename):
    '''
    调用mako，输出内容到指定页面。
    '''
    web.header('Content-type', 'text/html')
    mylookup = TemplateLookup(directories=[filepath], \
                              input_encoding='utf-8', \
                              default_filters=['decode.utf8'], \
                              output_encoding='utf-8', \
                              encoding_errors='replace')
    mytemplate = mylookup.get_template(filename + ".html")
    return mytemplate.render_unicode(v=objs).encode('utf-8', 'replace')


def fmtDate(timestamp, fmt):
    '''
    格式化时间。
    '''
    _format = "";
    if fmt == "DEFAULT":
        _format = '%Y-%m-%d %H:%M:%S'
    elif fmt == "STANDARD":
        _format = '%a, %d %b %Y %H:%M:%S %Z';
    elif fmt == 'MEDIUM':
        _format = '%b %d, %Y %H:%M %p';
    elif fmt == 'LONG':
        _format = '%A, %B %d, %Y %H:%M %p';
    elif fmt == 'SHORT':
        _format = '%b %d, %Y';
    elif fmt == '':
        _format = '%y-%m-%d %H:%M'
    else:
        _format = fmt
    return parse(timestamp).strftime(_format)
